﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

namespace HSIA
{
    public partial class Frm_DataDelete : Form
    {
        DatabaseObject dbo;
        public Frm_DataDelete(DatabaseObject pdbo)
        {
            InitializeComponent();
            dbo = pdbo;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool blColumn = false;
            DialogResult dlr = MessageBox.Show(dbo.GetLanguage(32));
            if (dlr == DialogResult.Cancel) return;
            string sSQL = "";
            string sDeleteFieldSQL = "";
            foreach (Control cl in this.Controls)
            {
                CheckBox cb = cl as CheckBox;
                if ((cb != null) && (cb.Tag != null)&&(cb.Checked))
                {
                    string sTable=cb.Tag.ToString();
                    if (sTable.IndexOf(",") > -1)
                    {
                        string[] sl = sTable.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string s in sl)
                        {
                            sSQL = sSQL + "truncate table " + s + ";";
                            if (s == "Area") sSQL = sSQL + "delete from AreaImage where ID<>'000';";
                        }
                    }
                    else
                    {
                        if (sTable == "Users") { sSQL = sSQL + "delete users where Tag='0' ;"; }
                        else
                        {
                            if (sTable == "GatherData") { sSQL = sSQL + "truncate table GatherData; truncate table GatherNo; truncate table NoGatherData;"; }
                            else
                            {
                                sSQL = sSQL + "truncate table " + sTable + ";";
                            }
                        }
                    }
                    if ((sTable == "LocationTitle") || (sTable == "SensorTitle") || (sTable == "MeasureType")) blColumn = true;
                    if (sTable == "SensorTitle")
                    {
                        DataTable dtSensorTitle = dbo.GetDataTableBySql("select FieldName from SensorTitle", "FieldName");
                        foreach (DataRow dr in dtSensorTitle.Rows)
                        {
                            sDeleteFieldSQL = sDeleteFieldSQL + PublicFunc.DropField("Sensor", dr["FieldName"].ToString());
                        }
                    }
                    //if (sTable == "MeasureType")
                    //{
                    //    DataTable dtMeasureType = dbo.GetDataTableBySql("select ID from MeasureType", "MeasureType");
                    //    foreach (DataRow dr in dtMeasureType.Rows)
                    //    {
                    //        string sID=dr["ID"].ToString();
                    //        sDeleteFieldSQL = sDeleteFieldSQL;// +PublicFunc.DropField("GatherData", sID) + PublicFunc.DropField("Sensor", "L" + sID) + PublicFunc.DropField("Sensor", "U" + sID);
                    //    }
                    //}
                    if (sTable == "LocationTitle")
                    {
                        DataTable dtLocationTitle = dbo.GetDataTableBySql("select ID from LocationTitle", "LocationTitle");
                        foreach (DataRow dr in dtLocationTitle.Rows)
                        {
                            string sID = dr["ID"].ToString();
                            sDeleteFieldSQL = sDeleteFieldSQL + PublicFunc.DropField("Sensor", sID);
                        }
                    }
                }
            }
            if (blColumn) sSQL = sSQL + "truncate table ColumnParam;" + sDeleteFieldSQL;
            string sRet=dbo.ExcuteSQL(sSQL);
            if (sRet == "OK")
            {
                //if ((chkMeasureType.Checked) || (chkSensorTitle.Checked)) PublicFunc.ReBuildViewSensor(dbo);
                sRet = dbo.GetLanguage(73);
                //if (blColumn) PublicFunc.ReBuildViewSensor(dbo);
            }
            MessageBox.Show(sRet);
            Close();
        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            foreach (Control cl in this.Controls)
            {
                CheckBox cb=cl as CheckBox;
                if (cb != null)
                {
                    if (cb.Tag != null) cb.Checked = checkBox9.Checked;
                }
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void FrmDeleteData_Load(object sender, EventArgs e)
        {
            this.Text = dbo.GetLanguage(376);//清理数据

            checkBox8.Text = dbo.GetLanguage(61);//采集数据
            checkBox1.Text = dbo.GetLanguage(237);//清理数据
            checkBox2.Text = dbo.GetLanguage(263);//清理数据
            checkBox3.Text = dbo.GetLanguage(33);//区域
            checkBox5.Text = dbo.GetLanguage(174);//地图
            chkSensorTitle.Text = dbo.GetLanguage(269);//传感器标题
            checkBox4.Text = dbo.GetLanguage(158);//区域级别
            checkBox7.Text = dbo.GetLanguage(250);//参照器
            checkBox6.Text = dbo.GetLanguage(335);//登录用户
            checkBox9.Text = dbo.GetLanguage(14);//全部

            //label1.Text = dbo.GetLanguage(180);
            //label2.Text = dbo.GetLanguage(204);
            button1.Text = dbo.GetLanguage(363);
            button2.Text = dbo.GetLanguage(51);
        }
    }
}
